//
//  WmcTeamCloudAlbumManagementVM.swift
//  YPWatermarkCamera
//
//  Created by 赵洋 on 2022/9/8.
//

import RxRelay
import RxSwift
import UIKit

class WmcTeamCloudAlbumManagementVM: WmcBaseVM {
    /// 团队相册
    var teamAblum: WmcMediaTeamAlbumModel?

    /// 团队信息
    let teamInfo: BehaviorRelay<WmcMediaTeamDetailModel?> = .init(value: nil)

    /// 错误
    let error: BehaviorRelay<Error?> = .init(value: nil)

    /// 是否开启同步
    let isSyn: BehaviorRelay<Bool> = .init(value: false)

    /// 复制团队号
    /// - Parameter teamId: teamId description
    /// - Returns: description
    func copyTeamId() {
        guard let teamId = teamAblum?.teamOpenId else { return }
        let pasteboard = WmcPasteboard()
        pasteboard.plainText = teamId
        pasteboard.saveToPasteboard()
        WmcHudManager.toast("复制成功")
    }

    /// 退出团队
    func quitTeam(succeed: (() -> Void)?) {
        guard let teamId = teamAblum?.teamId, let succeed = succeed else { return }
        WmcHudManager.loading()
        let uid = WmcLoginManager.shared.wmcAccountModel?.yupaoId ?? 0
        WmcMediaTeamManagerAPI.exitTeam(teamId: teamId, uid: uid).subscribe { [weak self] _ in
            guard let weakSelf = self else { return }
            WmcHudManager.dismiss()

            /// 同步配置
            let config = WmcMediaAlbumConfigurationModel()
            config.albumType = 1
            config.isSync = true
            config.name = weakSelf.teamInfo.value?.teamName ?? ""
            config.albumId = weakSelf.teamInfo.value?.teamId ?? 0
            config.busId = weakSelf.teamInfo.value?.teamId ?? 0
            WmcPreferenceSettingsManager.addTeamSyncConfig(model: config)
            succeed()
        } onError: { error in
            WmcHudManager.toast(error)
            WmcHudManager.dismiss()
        }.disposed(by: rx.disposeBag)
    }

    /// 获取团队信息
    /// - Returns: description
    func getTeamInfo() {
        guard let teamId = teamAblum?.teamId else { return }
        WmcHudManager.loading()
        WmcMediaTeamManagerAPI.fetchTeamInfo(teamId: teamId).subscribe { [weak self] team in
            guard let weakSelf = self else { return }
            weakSelf.teamAblum?.name = team.teamName
            weakSelf.teamInfo.accept(team)
            WmcHudManager.dismiss()
        } onError: { [weak self] error in
            guard let weakSelf = self else { return }
            WmcHudManager.toast(error)
            WmcHudManager.dismiss()
            weakSelf.error.accept(error)
        }.disposed(by: rx.disposeBag)
    }

    /// 修改同步配置
    /// - Returns: description
    func updateSynConfig(isSyn: Bool, succeed: ((_ isSyn: Bool) -> Void)?) {
        guard let albumId = teamAblum?.albumId, let teamId = teamAblum?.teamId else { return }
        WmcHudManager.loading()
        WmcMediaTeamManagerAPI.updateConfiguration(albumId: albumId, isOpen: isSyn, teamId: teamId).subscribe { [weak self] _ in
            guard let weakSelf = self, let succeed = succeed else { return }
            WmcHudManager.dismiss()
            /// 同步配置
            let config = WmcMediaAlbumConfigurationModel()
            config.albumType = 1
            config.isSync = true
            config.name = weakSelf.teamInfo.value?.teamName ?? ""
            config.albumId = weakSelf.teamInfo.value?.teamId ?? 0
            config.busId = weakSelf.teamInfo.value?.teamId ?? 0
            WmcPreferenceSettingsManager.addTeamSyncConfig(model: config)
            succeed(isSyn)
            WmcNotificationManager.shared.post(type: .synSettingChanged, object: nil)
        } onError: { error in
            WmcHudManager.toast(error)
            WmcHudManager.dismiss()
            guard let succeed = succeed else {
                return
            }
            succeed(false)
        }.disposed(by: rx.disposeBag)
    }

    /// 团队相册是否为自己创建
    func isTeamCreatedBySelf() -> Observable<Bool> {
        return RxSwift.Observable.create { [weak self] ob in
            guard let weakSelf = self else { return Disposables.create() }
            if let teamAblum = weakSelf.teamAblum {
                let userId = WmcLoginManager.shared.wmcAccountModel?.yupaoId ?? 0
                let isSelf = (teamAblum.createdUid == "\(userId)")
                ob.onNext(isSelf)
            }
            return Disposables.create()
        }
    }
}
